﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using DHQGHN.DAL;
using DHQGHN.DAL.BO.DanhMuc;
using DHQGHN.DAL.Models.ObjectReturn;
using DHQGHN.DAL.ModelsUpload;

namespace DHQGHN.Services.TDT
{
    public class UploadDataFromClientService : IUploadDataFromClientService
    {    
        private AdministrativeUnitExtBO administrativeUnitExtBO;
        private AddressBO addressBO;
        private HouseHoldBO householdBO;
        private ResidentBO residentBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UploadDataFromClientService));
        #region Constructors
        public UploadDataFromClientService()
        {
            //administrativeUnitExtBOAfter = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_AFTER_TDT_CONNECTION_STRING);;
            //addressBOAfter = new AddressBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_AFTER_TDT_CONNECTION_STRING); ;
            //householdBOAfter = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_AFTER_TDT_CONNECTION_STRING); ;
            //residentBOAfter = new ResidentBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_AFTER_TDT_CONNECTION_STRING); ;         
        }
        #endregion

        #region Service Method
        //VUONGBD
        //Them ADM_UNIT_EXT, ADDRESS, HOUSEHOLD, RESIDENT
        public JSONMessageTypeDataUpload AddDataFromClient(TemplateUploadFromClient data)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new JSONMessageTypeDataUpload();
                }
                else
                {
                    #region Khoi Tao
                    administrativeUnitExtBO = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    addressBO = new AddressBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    householdBO = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    residentBO = new ResidentBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    #endregion

                    JSONMessageTypeDataUpload jsonReturn = new JSONMessageTypeDataUpload();
                    JSONMessageType1 json = null;
                    string parameterJson = "";

                    #region SAVE ADM_UNIT_EXT
                    List<ADM_UNIT_EXT_UPLOAD> lstADMUnitExt = new List<ADM_UNIT_EXT_UPLOAD>();
                    if (data != null && data.ADM_UNIT_EXT.Count > 0)
                    {
                        lstADMUnitExt = data.ADM_UNIT_EXT;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstADMUnitExt);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = administrativeUnitExtBO.SaveAdmUnitExtFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.ADM_UNIT_EXT.Add(json);
                            }
                        }
                    }
                    #endregion

                    #region SAVE ADDRESS
                    List<ADDRESS_UPLOAD> lstAddress = new List<ADDRESS_UPLOAD>();
                    parameterJson = "";
                    if (data != null && data.ADDRESS.Count > 0)
                    {
                        lstAddress = data.ADDRESS;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstAddress);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = addressBO.SaveAddressFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.ADDRESS.Add(json);
                            }
                        }
                    }
                    #endregion

                    #region SAVE HOUSE HOLD
                    List<HOUSEHOLD_UPLOAD> lstHouseHold = new List<HOUSEHOLD_UPLOAD>();
                    parameterJson = "";
                    if (data != null && data.HOUSEHOLD.Count > 0)
                    {
                        lstHouseHold = data.HOUSEHOLD;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstHouseHold);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = householdBO.SaveHouseHoldFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.HOUSEHOLD.Add(json);
                            }
                        }
                    }
                    #endregion

                    #region SAVE RESIDENT
                    List<RESIDENT_UPLOAD> lstResident = new List<RESIDENT_UPLOAD>();
                    parameterJson = "";
                    if (data != null && data.RESIDENT.Count > 0)
                    {
                        lstResident = data.RESIDENT;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstResident);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = residentBO.SaveResidentFromJSON(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.RESIDENT.Add(json);
                            }
                        }
                    }
                    #endregion

                    return jsonReturn;
                }
            }catch(Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        //VUONGBD
        //Them ADM_UNIT_EXT, ADDRESS, HOUSEHOLD, RESIDENT
        public JSONMessageTypeDataUpload AddDataFromClientTest(TemplateUploadFromClient data)
        {
            try {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new JSONMessageTypeDataUpload();
                }
                else
                {
                    #region Khoi Tao
                    administrativeUnitExtBO = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    addressBO = new AddressBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    householdBO = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    residentBO = new ResidentBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    #endregion

                    JSONMessageTypeDataUpload jsonReturn = new JSONMessageTypeDataUpload();
                    JSONMessageType1 json = null;

                    #region SAVE ADM_UNIT_EXT
                    List<ADM_UNIT_EXT_UPLOAD> lstADMUnitExt = new List<ADM_UNIT_EXT_UPLOAD>();
                    if (data != null && data.ADM_UNIT_EXT.Count > 0)
                    {
                        lstADMUnitExt = data.ADM_UNIT_EXT;
                        foreach (ADM_UNIT_EXT_UPLOAD admUp in lstADMUnitExt)
                        {
                            json = new JSONMessageType1();
                            TableReturn resADM = administrativeUnitExtBO.SaveFromTabletNotePaper(admUp);
                            if ("SUCCESS".Equals(resADM.STATUS_ERROR))
                            {
                                json.ErrorCode = 200;
                                json.ErrorMessage = resADM.STATUS_ERROR;
                            }
                            else
                            {
                                json.ErrorCode = -1;
                                json.ErrorMessage = resADM.ERROR_MESSAGE;
                            }

                            json.ID = resADM.ID;
                            jsonReturn.ADM_UNIT_EXT.Add(json);
                        }
                    }
                    #endregion

                    #region SAVE ADDRESS
                    List<ADDRESS_UPLOAD> lstAddress = new List<ADDRESS_UPLOAD>();
                    if (data != null && data.ADDRESS.Count > 0)
                    {
                        lstAddress = data.ADDRESS;
                        foreach (ADDRESS_UPLOAD addUp in lstAddress)
                        {
                            json = new JSONMessageType1();
                            TableReturn resADM = addressBO.SaveFromTabletNotePaper(addUp);
                            if ("SUCCESS".Equals(resADM.STATUS_ERROR))
                            {
                                json.ErrorCode = 200;
                                json.ErrorMessage = resADM.STATUS_ERROR;
                            }
                            else
                            {
                                json.ErrorCode = -1;
                                json.ErrorMessage = resADM.ERROR_MESSAGE;
                            }

                            json.ID = resADM.ID;
                            jsonReturn.ADDRESS.Add(json);
                        }
                    }
                    #endregion

                    #region SAVE HOUSE HOLD
                    List<HOUSEHOLD_UPLOAD> lstHouseHold = new List<HOUSEHOLD_UPLOAD>();
                    if (data != null && data.HOUSEHOLD.Count > 0)
                    {
                        lstHouseHold = data.HOUSEHOLD;
                        foreach (HOUSEHOLD_UPLOAD hhUp in lstHouseHold)
                        {
                            json = new JSONMessageType1();
                            TableReturn resADM = householdBO.SaveFromTabletNotePaper(hhUp);
                            if ("SUCCESS".Equals(resADM.STATUS_ERROR))
                            {
                                json.ErrorCode = 200;
                                json.ErrorMessage = resADM.STATUS_ERROR;
                            }
                            else
                            {
                                json.ErrorCode = -1;
                                json.ErrorMessage = resADM.ERROR_MESSAGE;
                            }

                            json.ID = resADM.ID;
                            jsonReturn.HOUSEHOLD.Add(json);
                        }
                    }
                    #endregion

                    #region SAVE RESIDENT
                    List<RESIDENT_UPLOAD> lstResident = new List<RESIDENT_UPLOAD>();
                    if (data != null && data.RESIDENT.Count > 0)
                    {
                        lstResident = data.RESIDENT;
                        foreach (RESIDENT_UPLOAD rsUp in lstResident)
                        {
                            json = new JSONMessageType1();
                            TableReturn resADM = residentBO.SaveFromTabletNotePaper(rsUp);
                            if ("SUCCESS".Equals(resADM.STATUS_ERROR))
                            {
                                json.ErrorCode = 200;
                                json.ErrorMessage = resADM.STATUS_ERROR;
                            }
                            else
                            {
                                json.ErrorCode = -1;
                                json.ErrorMessage = resADM.ERROR_MESSAGE;
                            }

                            json.ID = resADM.ID;
                            jsonReturn.RESIDENT.Add(json);
                        }
                    }
                    #endregion

                    return jsonReturn;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        //VUONGBD
        //Them ADM_UNIT_EXT, ADDRESS

        public JSONMessageTypeDataADMAddressUpload AddAdmUnitExtAndAddress(TemplateUploadADMAddressFromClient data)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new JSONMessageTypeDataADMAddressUpload();
                }
                else
                {
                    #region Khoi Tao
                    administrativeUnitExtBO = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    addressBO = new AddressBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    #endregion

                    JSONMessageTypeDataADMAddressUpload jsonReturn = new JSONMessageTypeDataADMAddressUpload();
                    JSONMessageType1 json = null;
                    string parameterJson = "";

                    #region SAVE ADM_UNIT_EXT
                    List<ADM_UNIT_EXT_UPLOAD> lstADMUnitExt = new List<ADM_UNIT_EXT_UPLOAD>();
                    if (data != null && data.ADM_UNIT_EXT.Count > 0)
                    {
                        lstADMUnitExt = data.ADM_UNIT_EXT;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstADMUnitExt);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = administrativeUnitExtBO.SaveAdmUnitExtFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.ADM_UNIT_EXT.Add(json);
                            }
                        }
                    }
                    #endregion

                    #region SAVE ADDRESS
                    List<ADDRESS_UPLOAD> lstAddress = new List<ADDRESS_UPLOAD>();
                    parameterJson = "";
                    if (data != null && data.ADDRESS.Count > 0)
                    {
                        lstAddress = data.ADDRESS;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstAddress);
                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        List<TableReturn> lstObj = new List<TableReturn>();
                        lstObj = addressBO.SaveAddressFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturn obj in lstObj)
                            {
                                json = new JSONMessageType1();
                                if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                {
                                    json.ErrorCode = 200;
                                    json.ErrorMessage = obj.STATUS_ERROR;
                                }
                                else
                                {
                                    json.ErrorCode = -1;
                                    json.ErrorMessage = obj.ERROR_MESSAGE;
                                }
                                json.ID = obj.ID;
                                jsonReturn.ADDRESS.Add(json);
                            }
                        }
                    }
                    #endregion

                    return jsonReturn;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public JSONMessageTypeDataHouseHoldResident AddHouseHoldAndResident(TemplateUploadHouseHoldResidentFromClientForPM3 data)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new JSONMessageTypeDataHouseHoldResident();
                }
                else
                {
                    #region Khoi Tao
                    householdBO = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);

                    JSONMessageTypeDataHouseHoldResident jsonReturn = new JSONMessageTypeDataHouseHoldResident();
                    JSONMessageType3 json = null;
                    JSONMessageType1 jsonResident = null;
                    string parameterJson = "";
                    List<TableReturnForReturnInsertHouseHoldResident> lstObj;

                    List<TableReturnForReturnInsertHouseHoldResident> tmpList = new List<TableReturnForReturnInsertHouseHoldResident>();
                    #endregion

                    List<HOUSEHOLD_UPLOAD_TYPE3> lstHouseHold = new List<HOUSEHOLD_UPLOAD_TYPE3>();

                    #region SAVE HOUSE HOLD - RESDIENT
                    lstHouseHold = new List<HOUSEHOLD_UPLOAD_TYPE3>();
                    parameterJson = "";
                    if (data != null && data.HOUSEHOLD.Count > 0)
                    {
                        lstHouseHold = data.HOUSEHOLD;
                    }
                    parameterJson = DHQGHN.Common.Utils.ParseJSONFromListObject(lstHouseHold);

                    if (!DHQGHN.Common.Utils.IsNullOrEmptyOverride(parameterJson))
                    {
                        lstObj = new List<TableReturnForReturnInsertHouseHoldResident>();
                        lstObj = householdBO.SaveHouseHoldAndResidentFromJson(parameterJson);
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            foreach (TableReturnForReturnInsertHouseHoldResident obj in lstObj)
                            {
                                json = new JSONMessageType3();
                                if (TableName.HOUSEHOLD.Equals(obj.TABLE_NAME))
                                {
                                    json.ID = obj.ID;

                                    if ("SUCCESS".Equals(obj.STATUS_ERROR))
                                    {
                                        json.ErrorCode = 200;
                                        json.ErrorMessage = obj.STATUS_ERROR;
                                    }
                                    else
                                    {
                                        json.ErrorCode = -1;
                                        json.ErrorMessage = obj.ERROR_MESSAGE;
                                    }

                                    tmpList = GetByCondition(lstObj, json.ID);
                                    if (tmpList != null && tmpList.Count > 0)
                                    {
                                        foreach (TableReturnForReturnInsertHouseHoldResident tmpobj in tmpList)
                                        {
                                            jsonResident = new JSONMessageType1();
                                            if ("SUCCESS".Equals(tmpobj.STATUS_ERROR))
                                            {
                                                jsonResident.ErrorCode = 200;
                                                jsonResident.ErrorMessage = tmpobj.STATUS_ERROR;
                                            }
                                            else
                                            {
                                                jsonResident.ErrorCode = -1;
                                                jsonResident.ErrorMessage = tmpobj.ERROR_MESSAGE;
                                            }
                                            jsonResident.ID = tmpobj.ID;
                                            json.RESIDENT.Add(jsonResident);
                                            lstObj = Detele(lstObj, tmpobj);
                                        }
                                    }
                                }

                                if (json.ID != null)
                                {
                                    jsonReturn.HOUSEHOLD.Add(json);
                                }
                            }
                        }
                    }
                    #endregion
                    return jsonReturn;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }


        public List<TableReturnForReturnInsertHouseHoldResident> GetByCondition(List<TableReturnForReturnInsertHouseHoldResident> lst, string householdID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<TableReturnForReturnInsertHouseHoldResident>();
                }
                else
                {
                    //x => x.FirstName != "Bob"
                    //.Where(s => countryCodes.Contains(s.CountryCode))
                    List<TableReturnForReturnInsertHouseHoldResident> ret = new List<TableReturnForReturnInsertHouseHoldResident>();
                    if (lst != null && lst.Count > 0)
                    {
                        ret = lst.Where(a => a.TABLE_NAME == TableName.RESIDENT).ToList();
                        if (ret != null && ret.Count > 0)
                        {
                            ret = ret.Where(a => a.PARENT_ID == householdID).ToList();

                            if (ret.Count == 0)
                            {
                                ret = lst.Where(a => a.TABLE_NAME == TableName.RESIDENT).ToList();
                                ret = ret.Where(a => a.PARENT_ID.Equals("-1")).ToList();
                            }
                        }
                    }
                    return ret;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public List<TableReturnForReturnInsertHouseHoldResident> Detele(List<TableReturnForReturnInsertHouseHoldResident> sour , TableReturnForReturnInsertHouseHoldResident des)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<TableReturnForReturnInsertHouseHoldResident>();
                }
                else
                {
                    //var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));
                    List<TableReturnForReturnInsertHouseHoldResident> ret = new List<TableReturnForReturnInsertHouseHoldResident>();
                    if (des != null && sour != null && sour.Count > 0)
                    {
                        ret = sour.Where(a => a.ID != des.ID).ToList();
                    }
                    return ret;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

        }

        public JSONMessageTypeDataHouseHoldResident AddHouseHoldAndResidentTest(TemplateUploadHouseHoldResidentFromClient data)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new JSONMessageTypeDataHouseHoldResident();
                }
                else
                {
                    #region Khoi Tao
                    householdBO = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
                    residentBO = new ResidentBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);

                    JSONMessageTypeDataHouseHoldResident jsonReturn = new JSONMessageTypeDataHouseHoldResident();

                    JSONMessageType3 jsonHouseHold = null;
                    JSONMessageType1 jsonResident = null;
                    #endregion

                    List<HOUSEHOLD_UPLOAD_TYPE2> lstHouseHold = new List<HOUSEHOLD_UPLOAD_TYPE2>();

                    #region SAVE HOUSE HOLD - RESDIENT
                    lstHouseHold = new List<HOUSEHOLD_UPLOAD_TYPE2>();
                    if (data != null && data.HOUSEHOLD.Count > 0)
                    {
                        lstHouseHold = data.HOUSEHOLD;
                        foreach (HOUSEHOLD_UPLOAD_TYPE2 hh in lstHouseHold)
                        {
                            jsonHouseHold = new JSONMessageType3();
                            TableReturn rsHH = householdBO.SaveFromTabletNotePaper(hh);
                            if ("SUCCESS".Equals(rsHH.STATUS_ERROR))
                            {
                                jsonHouseHold.ErrorCode = 200;
                                jsonHouseHold.ErrorMessage = rsHH.STATUS_ERROR;

                                foreach (RESIDENT_UPLOAD resident in hh.RESIDENT)
                                {
                                    jsonResident = new JSONMessageType1();
                                    TableReturn rsRD = residentBO.SaveFromTabletNotePaper(resident);
                                    if ("SUCCESS".Equals(rsRD.STATUS_ERROR))
                                    {
                                        jsonResident.ErrorCode = 200;
                                        jsonResident.ErrorMessage = rsRD.STATUS_ERROR;
                                    }
                                    else
                                    {
                                        jsonResident.ErrorCode = -1;
                                        jsonResident.ErrorMessage = rsRD.ERROR_MESSAGE;
                                    }
                                    jsonResident.ID = rsRD.ID;
                                    jsonHouseHold.RESIDENT.Add(jsonResident);
                                }
                            }
                            else
                            {
                                jsonHouseHold.ErrorCode = -1;
                                jsonHouseHold.ErrorMessage = rsHH.ERROR_MESSAGE;
                            }
                            jsonHouseHold.ID = rsHH.ID;
                        }
                        jsonReturn.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                        jsonReturn.HOUSEHOLD.Add(jsonHouseHold);
                    }
                    #endregion
                    return jsonReturn;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        #endregion
    }
}